1 Data Management

Read the two datasets from the repository on Github.
  • Extracted on year 2020 presidential election data.
  • Only included Democrats and Republican party votes.
  • Included variables: state_po, county_name, county_fips, party, candidatevotes.
  • Merged the above dataset election.final with FIPS to Geocode Dataset fips.code using the FIPS as the primary key.

  • election.data <- read.csv("https://raw.githubusercontent.com/VigneshReddy79/STA553-DataViz/main/Datasets/countypresidential_election_2000-2020.csv")
    fips.code <- read.csv("https://raw.githubusercontent.com/VigneshReddy79/STA553-DataViz/main/Datasets/fips2geocode.csv")
    
    election.2020 <- election.data %>%
      filter( year == 2020 & (party == "DEMOCRAT" | party == "REPUBLICAN") ) %>%
      mutate( votes.percent = round((candidatevotes/totalvotes)*100, 2) )
      
    election.final <- election.2020 %>%
      group_by( state_po, county_fips) %>%
      summarise( highest.votespercent = max(votes.percent, na.rm = TRUE) )
    
    election.final <- merge( election.2020, election.final, by = "county_fips", all.x = FALSE)
    
    election.final <- election.final %>%
      filter( votes.percent == highest.votespercent )
    
    election_fips <- merge(election.final, fips.code, by.x = "county_fips", by.y = "fips", all.x =                        FALSE)
    
    election_fips <- election_fips %>%
      mutate( republican = ifelse(party == "REPUBLICAN", yes = 1, no = 0)) %>%
      mutate(state = state_po.x, party_won = party) %>%
      select(state, county_fips, county_name, party_won, candidatevotes, totalvotes, votes.percent,          republican, lat, lon)
    
    election_fips$county_fips <-  ifelse ((election_fips$county_fips < 10000),
      yes = paste(0,election_fips$county_fips, sep = ""), no = election_fips$county_fips)
    
    saveRDS(election_fips, file="D:/OneDrive - West Chester University of PA/Documents/Spring'22/STA553-DataViz/git/Datasets/election_fips.RData")

    2 Choropleth map showing US Presidential Elections 2020 by county

    2.1 Interactive Choropleth map using Plotly function

  • Created an interactive choropleth map to display the presidential election results at county level using two different colors to represent the two parties.

  • url <- "https://github.com/pengdsci/sta553/raw/main/data/geojson-counties-fips.json"
    counties <- rjson::fromJSON(file=url)
    
    g <- list(scope = 'usa',
              projection = list(type = 'albers usa'),
              showlakes = TRUE,
              lakecolor = toRGB('white'))
    
    fig <- plot_ly()  %>% 
      add_trace( type = "choropleth",
              geojson = counties,
            locations = election_fips$county_fips,
                    z = election_fips$republican,
               colors = c("blue","red"),
                 zmin = 0,
                 zmax = 1,
                 text = ~paste("<br>County: ", election_fips$county_name,
                               "<br>Party won: ", election_fips$party,
                               "<br>Winner votes percent: ", election_fips$votes.percent,
                               "<br>Total votes: ", election_fips$totalvotes,
                               "<br>State: ", election_fips$state),
            hoverinfo = "text",
               marker = list(line=list(width=0.3)))   %>% 
      hide_colorbar() %>%
      layout( title = list(text = "<b>US Presidential elections 2020 by County</b>",
                           font = list(size = 20,
                                       color = "darkred")),
              margin = list( b = 15, l = 25, t = 85, r = 25),
              geo = g)
    fig

    2.2 Interactive Choropleth map using Plotly function #2

  • This is another way to represent the US presidential election 2020 data by county. Created an interactive choropleth map to display the presidential election results at county level using the color palette to represent the votes percent for party.

  • library("RColorBrewer")
    fig2 <- plot_ly()  %>% 
      add_trace( type = "choropleth",
              geojson = counties,
            locations = election_fips$county_fips,
                    z = election_fips$votes.percent,
           colorscale = "TealGrn",  
                 zmin = 0,
                 zmax = 100,
                 text = ~paste("<br>County: ", election_fips$county_name,
                               "<br>Party won: ", election_fips$party,
                               "<br>Winner votes percent: ", election_fips$votes.percent,
                               "<br>Total votes: ", election_fips$totalvotes,
                               "<br>State: ", election_fips$state),
            hoverinfo = "text",
               marker = list(line=list(width=0.3))) %>% 
      colorbar(title = "Winner Votes percent(%)") %>% 
      layout( title = list(text = "<b>US Presidential elections 2020 by County</b>",
                           font = list(size = 20,
                                       color = "darkred")),
              margin = list( b = 15, l = 25, t = 85, r = 25),
              geo = g)
    fig2

    2.3 Choropleth map using Tableau Public

    Using the Tableau, created the choropleth map as described in Part I of this assignment.
  • Created the map with Tableau and published it on Tableau’s public server.
  • Embeded the map into RMarkdown using the IMG tag from directly tableau public server link(hence embeded it to the knitted HTML file).
  • The link for the visualization created in Tableau public is: click here